package com.exercises.controller;

import com.exercises.object.Student;
import com.exercises.result.Result;
import com.exercises.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * --- 代码敲对！ ---
 *
 * @author CCCccc
 * @create 2025/8/15
 * 备注：学生控制层类，用来编写与学生相关的各种请求
 */
@Controller
@ResponseBody
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 根据id查询学生
     * */
    @GetMapping("/student/{id}")
    @ResponseBody
    public Result getStudentById(@PathVariable("id") int id){
        Student studentById = studentService.getStudentById(id);
        if (studentById != null){
            return Result.success(studentById);
        }else {
            return Result.fail(500, "查询失败",null);
        }
    }

    /**
     * 查询所有学生
     */
    @GetMapping("/student/all")
    @ResponseBody
    public Result getAllStudents(){
        return Result.success(studentService.getAllStudents());
    }

    /**
     * 根据年龄或姓名查询学生
     */
    @GetMapping("/student/search")
    @ResponseBody
    public Result getStudentByAgeOrName(@RequestParam HashMap<String, String> map){
        return Result.success(studentService.getStudentByAgeOrName(map));
    }

    /**
     * 添加学生
     */
    @PostMapping("/student/add")
    @ResponseBody
    public Result addStudent(@RequestBody Student student){
        boolean isok = studentService.addStudent(student);
        if (isok){
            return Result.fail(200, "添加成功",null);
        }else {
            return Result.fail(500, "添加失败",null);
        }
    }

    /**
     * 修改学生
     */
    @PutMapping("/student/update")
    @ResponseBody
    public Result updateStudent(@RequestBody Student student){
        System.out.println(student);
        boolean isok = studentService.updateStudent(student);
        if (isok){
            return Result.fail(200, "修改成功",null);
        }else {
            return Result.fail(500, "修改失败",null);
        }
    }

    /**
     * 批量删除学生
     */
    @DeleteMapping("/student/delete/batch")
    @ResponseBody
    public Result deleteStudentBatch(@RequestBody int[] ids){
        boolean isok = studentService.deleteStudentBatch(ids);
        if (isok){
            return Result.fail(200, "批量删除成功",null);
        }else {
            return Result.fail(500, "批量删除失败",null);
        }
    }

    /**
     * 查询所有学生以及对应的教室信息
     */
    @GetMapping("/student/classroom")
    @ResponseBody
    public Result getStudentWithClassroom(){
        return Result.success(studentService.getStudentWithClassroom());
    }

    /**
     * 查询所有学生以及对应的学科与任课老师
     */
    @GetMapping("/student/subject")
    @ResponseBody
    public Result getStudentWithSubject(){
        return Result.success(studentService.getStudentWithSubject());
    }

    /**
     * 查询所有学生以及对应的班级信息(单表)
     */
    @GetMapping("/student/classroomv2")
    @ResponseBody
    public Result getStudentWithClassroomv2(){
        return Result.success(studentService.getStudentWithClassroomv2());
    }

    /**
     * 根据id查询学生以及对应的学科信息(单表)
     */
    @GetMapping("/student/subjectv2/{id}")
    @ResponseBody
    public Result getStudentWithSubjectv2(@PathVariable("id") int id){
        return Result.success(studentService.getStudentWithSubjectv2(id));
    }

}
